import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import {
  ApprovalFlowTemplateVO,
  ApprovalFlowTemplateForm,
  ApprovalFlowTemplateQuery,
  ApprovalInstanceUpdateForm,
  ApprovalAuditBo,
  ApprovalInstanceVo,
  UserApprovalRecordsVo,
  UserApprovalStatisticsVo,
  ApprovalRecordVo,
  ApprovalInstanceForm
} from '@/api/other/approval/types';

/**
 * 查询审核流程模板列表
 */
export const listApprovalFlowTemplate = (query?: ApprovalFlowTemplateQuery): AxiosPromise<ApprovalFlowTemplateVO[]> => {
  return request({
    url: '/approval/flowTemplate/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询审核流程模板详细信息
 */
export const getApprovalFlowTemplate = (id: string | number): AxiosPromise<ApprovalFlowTemplateVO> => {
  return request({
    url: '/approval/flowTemplate/' + id,
    method: 'get'
  });
};

/**
 * 新增审核流程模板
 */
export const addApprovalFlowTemplate = (data: ApprovalFlowTemplateForm) => {
  return request({
    url: '/approval/flowTemplate',
    method: 'post',
    data: data
  });
};

/**
 * 修改审核流程模板
 */
export const updateApprovalFlowTemplate = (data: ApprovalFlowTemplateForm) => {
  return request({
    url: '/approval/flowTemplate',
    method: 'put',
    data: data
  });
};

/**
 * 删除审核流程模板
 */
export const delApprovalFlowTemplate = (id: string | number | Array<string | number>) => {
  return request({
    url: '/approval/flowTemplate/' + id,
    method: 'delete'
  });
};

/**
 * 审核接口（推荐使用）
 */
export const audit = (data: ApprovalAuditBo) => {
  return request({
    url: '/approval/instance/audit',
    method: 'put',
    data: data
  });
};

/**
 * 更新审核实例状态
 */
export const updateApprovalInstance = (data: ApprovalInstanceUpdateForm) => {
  return request({
    url: '/approval/instance/update',
    method: 'put',
    data: data
  });
};

/**
 * 获取指定业务的审核流程展示信息
 */
export const displayApprovalFlow = (businessType: string, businessId: string | number): AxiosPromise<ApprovalInstanceVo> => {
  return request({
    url: '/approval/instance/display',
    method: 'get',
    params: {
      businessType,
      businessId
    }
  });
};

/**
 * 查询用户审核记录（待审核、已审核、已提交）
 */
export const getUserApprovalRecords = (businessType?: string, currentUser?: string | number): AxiosPromise<UserApprovalRecordsVo> => {
  return request({
    url: '/approval/instance/userRecords',
    method: 'get',
    params: {
      businessType,
      currentUser
    }
  });
};

/**
 * 查询用户审核记录统计信息
 */
export const getUserApprovalStatistics = (businessType?: string, currentUser?: string | number): AxiosPromise<UserApprovalStatisticsVo> => {
  return request({
    url: '/approval/instance/userStatistics',
    method: 'get',
    params: {
      businessType,
      currentUser
    }
  });
};

/**
 * 分页查询用户待审核记录
 */
export const getUserPendingApprovalsPage = (params?: {
  businessType?: string;
  currentUser?: string | number;
  pageNum?: number;
  pageSize?: number;
}): AxiosPromise<{ rows: ApprovalInstanceVo[]; total: number }> => {
  return request({
    url: '/approval/instance/userPendingPage',
    method: 'get',
    params
  });
};

/**
 * 分页查询用户已审核记录
 */
export const getUserCompletedApprovalsPage = (params?: {
  businessType?: string;
  currentUser?: string | number;
  pageNum?: number;
  pageSize?: number;
}): AxiosPromise<{ rows: ApprovalInstanceVo[]; total: number }> => {
  return request({
    url: '/approval/instance/userCompletedPage',
    method: 'get',
    params
  });
};

/**
 * 分页查询用户已提交记录
 */
export const getUserSubmittedApprovalsPage = (params?: {
  businessType?: string;
  currentUser?: string | number;
  pageNum?: number;
  pageSize?: number;
}): AxiosPromise<{ rows: ApprovalInstanceVo[]; total: number }> => {
  return request({
    url: '/approval/instance/userSubmittedPage',
    method: 'get',
    params
  });
};

/**
 * 获取启用的审核模板列表（用于筛选）
 */
export const getActiveTemplatesForQuery = (): AxiosPromise<ApprovalFlowTemplateVO[]> => {
  return request({
    url: '/approval/flowTemplate/listForQuery',
    method: 'get'
  });
};


/**
 * 获取启用的审核模板列表（用于筛选）
 */
export const queryUserRecordList = (instanceId: number) : Promise<ApprovalRecordVo[]> => {
  return request({
    url: '/approval/instance/queryUserRecordList',
    method: 'get',
    params: { instanceId }  // 作为 ?instanceId=xxx
  });
};


/**
 * 获取审批详情
 * @param businessType 业务类型
 * @param businessId   业务id
 */
export const displayApprovalDetails = (
  businessType: string,
  instanceId: number
): AxiosPromise<any> => {
  return request({
    url: '/approval/details/display',
    method: 'get',
    params: { businessType, instanceId }
  })
}

/**
 * 根据instanceId获取审批阶段
 */
export const queryUserStageList = (instanceId: number) : Promise<ApprovalRecordVo[]> => {
  return request({
    url: '/approval/instance/queryUserStageList',
    method: 'get',
    params: { instanceId }  // 作为 ?instanceId=xxx
  });
};

/**
 * 根据实例ID查询当前用户全部审核记录
 */
export const getApprovalInstance = (id: number, flag: number): Promise<ApprovalInstanceVo> => {
  return request({
    url: '/approval/instance/query',
    method: 'post',
    params: { id, flag }  // 对应 @RequestParam
  })
}


/**
 * 获取审批详情
 * @param businessType 业务类型
 * @param businessId   业务id
 */
export const queryJsonData = (
  businessType: string,
  businessId: number
): AxiosPromise<any> => {
  return request({
    url: '/approval/instance/getJsonData',
    method: 'post',
    params: { businessType, businessId }
  })
}

 /**
 * 分页查询审核实例列表
 */
export const listApprovalInstance = (query?: any): AxiosPromise<{ rows: ApprovalInstanceVo[]; total: number }> => {
  return request({
    url: '/approval/instance/page',
    method: 'get',
    params: query
  });
};

/**
 * 审核回退
 */
export const rollbackApproval = (instanceId: string | number): AxiosPromise<void> => {
  return request({
    url: `/approval/instance/rollback/${instanceId}`,
    method: 'put'
  });
};

/**
 * 审核回退到指定阶段
 */
export const rollbackApprovalToStage = (instanceId: string | number, stageOrder: number): AxiosPromise<void> => {
  return request({
    url: `/approval/instance/rollback/${instanceId}/${stageOrder}`,
    method: 'put'
  });
};

// 查询用户抄送记录
export function getUserCcApprovalsPage(query: any) {
  return request({
    url: '/approval/instance/userCcApprovalPage',
    method: 'get',
    params: query
  })
}